<template>
  <div class="page-container">
    <div class="operators">
      <el-button
        v-permission="['role:search']"
        type="primary"
        size="medium"
        @click="getRolesHandle"
        >刷新</el-button
      >
      <el-button
        v-permission="['role:add']"
        type="primary"
        size="medium"
        @click="addRoleDialog"
        >新增</el-button
      >
    </div>

    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      :height="tableHeight"
      align="center"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" align="center" label="名称">
      </el-table-column>
      <el-table-column prop="remark" align="center" label="备注">
        <template slot-scope="scope">{{ scope.row.remark || "---" }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permission="['role:delete']"
            @click="deleteRole(scope.row)"
            type="danger"
            size="small"
            >删除</el-button
          >
          <el-button
            @click="addRoleDialog(scope.row)"
            type="primary"
            v-permission="['role:update']"
            size="small"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <Edit
      title="角色"
      :formData="form"
      :dialogVisible="roleVisible"
      v-if="roleVisible"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import { getRoles, deleteRole } from "@/api/sys/role";
import { getMenus } from "@/api/sys/sys";
import Edit from "./edit.vue";
import { autoTableHeight } from "@/utils/tools";
export default {
  data() {
    return {
      form: {},
      dialogVisible: false,
      roleVisible: false,
      menuTree: [],
      tableData: [],
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  components: {
    Edit,
  },
  created() {
    autoTableHeight(220, (e) => {
      this.tableHeight = e;
    });
    this.getRolesHandle();
  },
  methods: {
    closeDialog(isRefresh) {
      this.roleVisible = false;
      this.form = {};
      isRefresh && this.getRolesHandle();
    },
    getRolesHandle() {
      getRoles().then((res) => {
        this.tableData = res.data;
      });
    },
    getMenusHandle() {
      getMenus().then((res) => {
        this.menuTree = res.data;
      });
    },
    deleteRole(role) {
      this.$confirm(`确认删除当前角色?`, "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRole({
            id: role.id,
          }).then((res) => {
            this.getRolesHandle();
          });
        })
        .catch(() => {});
    },
    addRoleDialog(user) {
      if (user.id) {
        this.form = user;
      }
      this.roleVisible = true;
    },
  },
};
</script>

<style scoped>
.operators {
  margin-bottom: 16px;
}
.el-cascader {
  width: 100%;
}
.dialog-btns {
  text-align: center;
}
</style>
